package com.wlc.service.impl;

import com.wlc.mapper.AttendanceStatsMapper;
import com.wlc.pojo.AttendanceStats;
import com.wlc.service.AttendanceStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AttendanceStatsServiceImpl implements AttendanceStatsService {

    @Autowired
    private AttendanceStatsMapper attendanceStatsMapper;

    @Override
    public List<AttendanceStats> getDeptAttendanceStats(String startDate, String endDate) {
        log.info("查询部门考勤统计: startDate={}, endDate={}", startDate, endDate);

        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            startDate = now.withDayOfMonth(1).toString(); // 本月第一天
            endDate = now.toString();
        }

        return attendanceStatsMapper.selectDeptAttendanceStats(startDate, endDate);
    }

    @Override
    public List<AttendanceStats> getPersonalAttendanceDetail(Long employeeId, String startDate, String endDate) {
        log.info("查询个人考勤明细: employeeId={}, startDate={}, endDate={}", employeeId, startDate, endDate);

        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            startDate = now.withDayOfMonth(1).toString(); // 本月第一天
            endDate = now.toString();
        }

        return attendanceStatsMapper.selectPersonalAttendanceDetail(employeeId, startDate, endDate);
    }

    @Override
    public List<AttendanceStats> getDailyAttendanceStats(String startDate, String endDate) {
        log.info("查询每日考勤统计: startDate={}, endDate={}", startDate, endDate);

        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            startDate = now.minusDays(30).toString(); // 最近30天
            endDate = now.toString();
        }

        return attendanceStatsMapper.selectDailyAttendanceStats(startDate, endDate);
    }

    @Override
    public List<AttendanceStats> getMonthlyAttendanceSummary(String year) {
        log.info("查询月度考勤汇总: year={}", year);

        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(LocalDate.now().getYear());
        }

        return attendanceStatsMapper.selectMonthlyAttendanceSummary(year);
    }

    @Override
    public Object getAttendanceOverview(String startDate, String endDate) {
        log.info("查询考勤综合统计: startDate={}, endDate={}", startDate, endDate);

        if (startDate == null || endDate == null) {
            LocalDate now = LocalDate.now();
            startDate = now.withDayOfMonth(1).toString(); // 本月第一天
            endDate = now.toString();
        }

        Map<String, Object> result = new HashMap<>();
        result.put("deptStats", getDeptAttendanceStats(startDate, endDate));
        result.put("dailyStats", getDailyAttendanceStats(startDate, endDate));
        result.put("monthlySummary", getMonthlyAttendanceSummary(String.valueOf(LocalDate.now().getYear())));

        // 计算总体考勤率
        List<AttendanceStats> deptStats = getDeptAttendanceStats(startDate, endDate);
        double totalAttendanceRate = deptStats.stream()
                .mapToDouble(stats -> stats.getAttendanceRate() != null ? stats.getAttendanceRate().doubleValue() : 0)
                .average()
                .orElse(0);
        result.put("overallAttendanceRate", Math.round(totalAttendanceRate * 100.0) / 100.0);

        return result;
    }
}